package com.zr.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.springframework.stereotype.Service;
import com.zr.dao.BaseDao;
import com.zr.dao.OrdersDao;
import com.zr.model.Djjg;
import com.zr.model.Func;
import com.zr.model.Hwlb;
import com.zr.model.Orders;
import com.zr.model.Ordertype;
import com.zr.model.Point;
import com.zr.model.Proxy;
import com.zr.model.Psfwb;
import com.zr.model.Psjg;
import com.zr.model.Status;
import com.zr.model.User;
import com.zr.service.OrderService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service("orderService")
public class OrderServiceImpl implements OrderService{

	@Resource
	private BaseDao<Object>  baseDao;
	@Resource
	private OrdersDao<Orders> orderDao;
	public JSONArray getAllPoint() {
		JSONArray js =new JSONArray();
		String hql = "from Point";
		List<Object>  obs = baseDao.find(hql);
		for (Object obj : obs) {
			Point point=(Point) obj;
			JSONObject j = new JSONObject();
			j.put("id", point.getPid());
			j.put("text", point.getPname());
			js.add(j);
			}
		return js;
	}
	@Override
	public JSONArray getAllProxy() {
		JSONArray js =new JSONArray();
		String hql = "from Psfwb";
		List<Object>  obs = baseDao.find(hql);
		for (Object obj : obs) {
			Psfwb pxy=(Psfwb) obj;
			JSONObject j = new JSONObject();
			j.put("id", pxy.getDid());
			j.put("text", pxy.getDname());
			js.add(j);
			}
		return js;
	}
	@Override
	public JSONArray getAllType() {
		JSONArray js =new JSONArray();
		String hql = "from Ordertype";
		List<Object>  obs = baseDao.find(hql);
		for (Object obj : obs) {
			Ordertype otype=(Ordertype) obj;
			JSONObject j = new JSONObject();
			j.put("id", otype.getId());
			j.put("text", otype.getType());
			js.add(j);
			}
		return js;
	}
	@Override
	public JSONArray getAllOstatus() {
		JSONArray js =new JSONArray();
		String hql = "from Status";
		List<Object>  obs = baseDao.find(hql);
		for (Object obj : obs) {
			Status otype=(Status) obj;
			JSONObject j = new JSONObject();
			j.put("id", otype.getId());
			j.put("text", otype.getStatus());
			js.add(j);
			}
		return js;
	}
	
	@Override
	public JSONArray getAllGoodsType() {
		JSONArray js =new JSONArray();
		String hql = "from Hwlb";
		List<Object>  obs = baseDao.find(hql);
		for (Object obj : obs) {
			Hwlb goods=(Hwlb) obj;
			JSONObject j = new JSONObject();
			j.put("id", goods.getId());
			j.put("text", goods.getName());
			js.add(j);
			}
		return js;
	}
	@Override
	public boolean addOrder(Orders order,User u) {
		order.setOrderstatus("未处理");
		order.setPaystate("未支付");
		order.setUid(u.getUid());
		baseDao.saveOrUpdate(order);
		return true;
	}
	@Override
	public JSONObject findYHOrder(User u, Integer page, Integer rows){
		String hql="select count(*) from Orders o where o.uid=?";
		long total= baseDao.count(hql,new Object[]{u.getUid()});
		String hql1="from Orders o where o.uid=?";
		List<Object> obs=baseDao.find(hql1,new Object[]{u.getUid()}, page, rows);		
		JSONObject jstu = new JSONObject();
		jstu.put("total", total);
		jstu.put("rows",obs);
		return jstu;
	}
	@Override
	public boolean removeOrder(Integer oid, String ostatus) {
		return orderDao.removeOrder(oid,ostatus);
	}
	@Override
	public JSONObject findAllOrder(Integer page, Integer rows) {
		String hql="select count(*) from Orders o ";
		long total= baseDao.count(hql);
		String hql1="from Orders o ";
		List<Object> obs=baseDao.find(hql1, new Object[]{}, page, rows);		
		JSONObject jstu = new JSONObject();
		jstu.put("total", total);
		jstu.put("rows",obs);
		return jstu;
	}
	@Override
	public JSONArray findOrder(Orders order,String strfhdate,String  overfhdate) {	
		List<Orders> obs = orderDao.findOrder(order,strfhdate,overfhdate);
		JSONArray jstu = new JSONArray();
		for (Orders o : obs) {		
			jstu.add(o);
		}
		return jstu;
	}
	@Override
	public boolean updateOrderState(Orders order) {
		return orderDao.updateOrderState(order);
	}
	@Override
	public int updateAndAddorder(Orders order) {
		return orderDao.updateAndAddorder(order);
	}
	@Override
	public JSONObject findPointOrder(User u,Integer page,Integer rows){
		//获取站点信息
		String hql0=" from Point p where p.uid=?";
		Point p =(Point)baseDao.get(hql0, new Object[]{u.getUid()});
		//查找目的代理点信息
		String hqly="from Psfwb p where p.pid=?";
		Psfwb ps =(Psfwb)baseDao.get(hqly, new Object[]{p.getPid()});
		//获取订单信息
		String hql="select count(*) from Orders o where o.place=? or o.receipt=? or o.receipt=?";
		long total= baseDao.count(hql,new Object[]{p.getPname(),ps.getDname(),p.getPname()});
		String hql1="from Orders o where o.place=? or o.receipt=?";
		List<Object> obs=baseDao.find(hql1,new Object[]{p.getPname(),ps.getDname()}, page, rows);		
		JSONObject jstu = new JSONObject();
		jstu.put("total", total);
		jstu.put("rows",obs);
		return jstu;
	}
//计算价格
	@Override
	public JSONObject getjiage(Orders order) {
		//查找目的代理点信息
		String hql="from Psfwb p where p.dname=?";
		Psfwb p =(Psfwb)baseDao.get(hql, new Object[]{order.getReceipt()});
		//查找目的配送点信息
		String hql1 ="from Point p where p.pid=?";
		Point po=(Point) baseDao.get(hql1, new Object[]{p.getPid()});		
//-------计算配送价格------
		String hql2 ="from Psjg p where p.pname=? and p.dname=?";
		Psjg pjg = (Psjg) baseDao.get(hql2, new Object[]{po.getPname(),p.getDname()});
		//重量算价格
		double weipic;
		double wei=Double.parseDouble(order.getWeight());
		if(wei<=1.0){
			weipic=wei*Double.parseDouble(pjg.getSgjjg());
		}else{
			weipic=(wei-1.0)*Double.parseDouble(pjg.getCgjjg())+Double.parseDouble(pjg.getSgjjg());
		}
		//体积算价格
		double volpic;
		double vol=Double.parseDouble(order.getVolume());
		if(vol<=1.0){
			volpic=vol*Double.parseDouble(pjg.getSlfjg());
		}else{
			volpic=(vol-1.0)*Double.parseDouble(pjg.getClfjg())+Double.parseDouble(pjg.getSlfjg());
		}
		//配送价格
		double pspic=weipic>volpic?weipic:volpic;
//-------计算运输价格------------		
		double yspic =0;
		if(order.getPlace().equals(po.getPname())){
			 yspic=0;
		}else{
		//查找起点与终点的点间价格
		String hql3 ="from Djjg d where (d.fhpname=? and d.shpname=?) or (d.fhpname=? and d.shpname=?)";
		Djjg dj =(Djjg) baseDao.get(hql3, new Object[]{order.getPlace(),po.getPname(),po.getPname(),order.getPlace()});
		//重量算价格
				double weipic1;
				if(wei<=1.0){
					weipic1=wei*Double.parseDouble(dj.getSgjjg());
				}else{
					weipic1=(wei-1.0)*Double.parseDouble(dj.getCgjjg())+Double.parseDouble(pjg.getSgjjg());
				}
				//体积算价格
				double volpic1;
				if(vol<=1.0){
					volpic1=vol*Double.parseDouble(dj.getSlfjg());
				}else{
					volpic1=(vol-1.0)*Double.parseDouble(dj.getClfjg())+Double.parseDouble(pjg.getSlfjg());
				}
				//运输价格
				yspic=weipic1>volpic1?weipic1:volpic1;
		}
		JSONObject jstu = new JSONObject();
		jstu.put("pspic",pspic);
		jstu.put("yspic",yspic);
		return jstu;
	}

}
